package com.hmall.item.Interceptor;

import com.hmall.item.ThreadLocal.LoginThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("authorization");
        System.out.println("请求头的authorization为: " + authorization);



        if (authorization == null || "".equals(authorization)) {
            return false;
        }
            Long parseLong = Long.parseLong(authorization);
            LoginThreadLocal.setUserId(parseLong);
            return true;




    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LoginThreadLocal.remove();
    }
}
